package com.beiming.kotlindemo.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.beiming.kotlindemo.Fruit
import com.beiming.kotlindemo.R
import com.beiming.kotlindemo.adapter.RecyclerViewFruitAdapter
import kotlinx.android.synthetic.main.activity_recyclerview.*

class RecyclerviewActivity : BaseActivity() {

    override fun getContentResId() = R.layout.activity_recyclerview

    override fun initView() {

    }

    override fun initData() {
        initFruits()
        val layoutManager = StaggeredGridLayoutManager(6, StaggeredGridLayoutManager.VERTICAL)
        recyclerview.layoutManager = layoutManager
        val adapter = RecyclerViewFruitAdapter(fruits)
        recyclerview.adapter = adapter
    }

    private val fruits = ArrayList<Fruit>()

    /**
     * repeat函数
     */
    private fun initFruits() {
        repeat(15) {
            fruits.add(Fruit(getRandomLengthString("Apple"), R.drawable.apple_pic))
            fruits.add(Fruit(getRandomLengthString("Banana"), R.drawable.banana_pic))
            fruits.add(Fruit(getRandomLengthString("Orange"), R.drawable.orange_pic))
            fruits.add(Fruit(getRandomLengthString("Watermelon"), R.drawable.watermelon_pic))
            fruits.add(Fruit(getRandomLengthString("Grape"), R.drawable.grape_pic))
            fruits.add(Fruit(getRandomLengthString("Pineapple"), R.drawable.pineapple_pic))
            fruits.add(Fruit(getRandomLengthString("Strawberry"), R.drawable.strawberry_pic))
            fruits.add(Fruit(getRandomLengthString("Cherry"), R.drawable.cherry_pic))
            fruits.add(Fruit(getRandomLengthString("Mango"), R.drawable.mango_pic))
        }
    }

    private fun getRandomLengthString(str: String): String {
        val n = (1..20).random()
        val builder = StringBuilder()
        repeat(n) {
            builder.append(str)
        }
        return builder.toString()
    }
}
